package com.gongye.courseselectionsystem.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.mapper.CourseMapper;
import com.gongye.courseselectionsystem.mapper.StudentMapper;
import com.gongye.courseselectionsystem.mapper.TeacherMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.pojo.po.Course;
import com.gongye.courseselectionsystem.pojo.po.Student;
import com.gongye.courseselectionsystem.pojo.po.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author baorongchuan
 */
@Component
@Slf4j
public class Check {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ChooseCourseMapper chooseCourseMapper;
    @Resource
    private CourseMapper courseMapper;

    public Check() {
    }

    /**
     * 判断学生表里是否存在输入的学生学号
     *
     * @param studentNumber 学生工号
     * @return true表示存在，false不存在
     */
    public boolean isExistStuNum(Integer studentNumber) {
        LambdaQueryWrapper<Student> studentWrapper = new LambdaQueryWrapper<>();
        studentWrapper.eq(Student::getStudentNumber, studentNumber);
        Student student = studentMapper.selectOne(studentWrapper);
        return student != null;
    }

    /**
     * 判断老师表里是否存在输入的教师工号
     *
     * @param teacherJobNumber 教师工号
     * @return true表示存在，false不存在
     */

    public boolean isExistJobNum(Integer teacherJobNumber) {
        LambdaQueryWrapper<Teacher> eq = new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherJobNumber, teacherJobNumber);
        List<Teacher> teachers = teacherMapper.selectList(eq);
        return teachers.size() >= 1;
    }

    /**
     * 判断选择的课程是不时已经选择过的
     *
     * @param studentNumber 学生学号
     * @param courseNumber  课程编号
     * @return 返回学生的课程信息
     */
    public ChooseCourse isSameCourse(Integer studentNumber, Integer courseNumber) {
        LambdaQueryWrapper<ChooseCourse> chooseCourseWrapper = new LambdaQueryWrapper<>();
        chooseCourseWrapper.eq(ChooseCourse::getStudentNumber, studentNumber);
        chooseCourseWrapper.eq(ChooseCourse::getCourseNumber, courseNumber);
        return chooseCourseMapper.selectOne(chooseCourseWrapper);
    }

    /**
     * 判断该学生是否选择了该老师的课程
     *
     * @param studentNumber       学生学号
     * @param oldTeacherJobNumber 老师工号
     * @return true表示学生已经选了课程 false表示没找到该学生选择该教师的选课记录
     */
    public boolean isExistChooseCourse(Integer studentNumber, Integer oldTeacherJobNumber) {
        LambdaQueryWrapper<ChooseCourse> eq = new LambdaQueryWrapper<ChooseCourse>()
                .eq(ChooseCourse::getTeacherJobNumber, oldTeacherJobNumber)
                .eq(ChooseCourse::getStudentNumber, studentNumber);
        List<ChooseCourse> teachers = chooseCourseMapper.selectList(eq);
        return teachers.size() >= 1;
    }

    /**
     * 判断学号，教师工号是否存在
     *
     * @param studentNumber       学号
     * @param oldTeacherJobNumber 教师工号
     * @throws Exception 错误信息
     */
    public void isExistNum(Integer studentNumber, Integer oldTeacherJobNumber) throws Exception {

        boolean existStuNum = isExistStuNum(studentNumber);
        if (!existStuNum) {
            throw new Exception("您输入的学号不存在");
        }
        boolean existJobNum = isExistJobNum(oldTeacherJobNumber);
        if (!existJobNum) {
            throw new Exception("您输入的教师工号不存在");
        }
        boolean existJobNumCourse = isExistChooseCourse(studentNumber, oldTeacherJobNumber);
        if (!existJobNumCourse) {
            throw new Exception("请确认您是否选择了该教师的课程");
        }
    }

    /**
     * 根据学生学号查询学生信息
     *
     * @param studentNumber 学生学号
     * @return 返回学生信息
     */
    public Student getStudentById(Integer studentNumber) {
        LambdaQueryWrapper<Student> studentWrapper = new LambdaQueryWrapper<>();
        studentWrapper.eq(Student::getStudentNumber, studentNumber);
        return studentMapper.selectOne(studentWrapper);
    }

    /**
     * 查询学生是否已经选择了两门课
     *
     * @param studentNumber 学生标号
     * @return 返回一个数值，代表此学生选择了几门课
     */
    public Integer queryTwo(Integer studentNumber) {
        LambdaQueryWrapper<ChooseCourse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChooseCourse::getStudentNumber, studentNumber);
        return chooseCourseMapper.selectCount(lambdaQueryWrapper);
    }

    /**
     * 判断老师的学生是否以达到三个
     *
     * @param teacherJobNumber 教师工号
     * @param courseNumber     课程编号
     * @return true表示达到三个，false表示没有达到三个
     */
    public boolean haveThree(Integer teacherJobNumber, Integer courseNumber) {
        LambdaQueryWrapper<ChooseCourse> eq = new QueryWrapper<ChooseCourse>().lambda().eq(ChooseCourse::getTeacherJobNumber, teacherJobNumber)
                .eq(ChooseCourse::getCourseNumber, courseNumber);
        List<ChooseCourse> chooseCourses = chooseCourseMapper.selectList(eq);
        int a = 3;
        return chooseCourses.size() >= a;
    }

    /**
     * 查询学生是否重复选择了一个老师的课程
     *
     * @param studentNumber    学生学号
     * @param teacherJobNumber 教师工号
     * @return 返回下一选课的学生表
     */
    public ChooseCourse haveTeacher(Integer studentNumber, Integer teacherJobNumber) {
        LambdaQueryWrapper<ChooseCourse> eq = new LambdaQueryWrapper<ChooseCourse>().eq(ChooseCourse::getStudentNumber, studentNumber)
                .eq(ChooseCourse::getTeacherJobNumber, teacherJobNumber);
        return chooseCourseMapper.selectOne(eq);
    }

    /**
     * 根据课程标号，查询课程信息
     *
     * @param courseNumber 课程编号
     * @return 返回课程信息
     */
    public Course getCourseById(Integer courseNumber, Integer teacherJobNumber) throws Exception {
        Course course = courseMapper.selectOne(new QueryWrapper<Course>().lambda().
                eq(Course::getCourseNumber, courseNumber).
                eq(Course::getTeacherJobNumber, teacherJobNumber));
        if (course == null) {
            log.info("课程标号没有对应的老师");
            throw new Exception("课程标号没有对应的老师");
        }
        return course;
    }
}
